package br.com.gerador.component;

import java.awt.Component;
import java.awt.Container;
import java.awt.Panel;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import br.com.gerador.icomponent.IGComponent;
import br.com.gerador.icomponent.IGPage;
import br.com.gerador.icomponent.IGProprierdade;
import br.com.gerador.main.Principal;

public class GPage extends JPanel implements IGPage,IGProprierdade {
	private Principal principal;
	
	public GPage() {
		super(null);
		addMouseListener(pageMouse);
	}
	
	public Principal getPrincipal() {
		return principal;
	}

	public void setPrincipal(Principal principal) {
		this.principal = principal;
	}

	MouseAdapter pageMouse = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			requestFocus();
			repaint();
		};

		public void mouseClicked(MouseEvent e) {
			montaPropriedades();
			super.mouseClicked(e);
		}
	};
	
	@Override
	public List<IGComponent> getGComponents() {
		List<IGComponent> comps = new ArrayList<IGComponent>();
		for(Component c: getComponents() ){
			if( c instanceof IGComponent ){
				comps.add((IGComponent)c);
			}
		}
		return comps;
	}
	
	public void montaPropriedades(){
		getPrincipal().getPropriedades().setComponent(this);
		getPrincipal().getPropriedades().bind();
	}
	
	@Override
	public void bindPropriedades() {
		for( String[] p : getPropriedades() ){
			if( p[2] != null ){
				firePropriedade(p[2], p[1]);
			}
		}
	}

	@Override
	public void firePropriedade(String method, Object value) {
		try {
			Method m = this.getClass().getMethod(method, new Class[]{String.class});
			m.invoke(this, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String[][] getPropriedades() {
		return propriedades;
	}

	@Override
	public void setPropriedades(String[][] values) {
		this.propriedades = values;
	}

	String[][] propriedades = new String[][]{
			{"ID","","id"},
			{"width","800","width"},
			{"height","600","height"}
		};
	
	public void id(String value){
		
	}
	
	public void width(String value){
		setBounds(getX(), getY(), Integer.parseInt(value), getHeight());
		getPrincipal().content.setSize(Integer.parseInt(value)+getX(), getHeight()+getY());
	}

	public void height(String value){
		setBounds(getX(), getY(), getWidth() ,Integer.parseInt(value));
		getPrincipal().content.setBounds(0, 0, getWidth()+getX(), Integer.parseInt(value)+getY());
		getPrincipal().repaint();
	}
}
